package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import connexionsBdd.Mysql;

import commande.Article;

public class DaoArticleMysql implements DaoArticle {
	private static final String SQL_SAVE = "insert into article (libelle,prixUnitaire) values(?,?)";
	private static final String SQL_GET = "SELECT * FROM article WHERE reference=? ";
	private static final String SQL_LOAD = "SELECT * FROM article WHERE libelle=?";
	private static final String SQL_UPDATE ="update article set libelle=?,prixUnitaire=? WHERE reference=?";
	private static final String SQL_DELETE = "delete from article where reference=?";



	@Override
	public Article get(int ref) {
		if(ref==-1)
			throw new IllegalArgumentException();
		try {
			PreparedStatement req = Mysql.getInstance().prepareStatement(SQL_GET);
			req.setInt(1,ref);
			ResultSet res = req.executeQuery();

			if(res.next())
			{
				Article a = new Article(res.getInt("reference"),res.getString("libelle"),res.getDouble("prixUnitaire"));
				return a;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public ArrayList<Article> load(String libelle) {
		// TODO Auto-generated method stub
		PreparedStatement req;
		try {
			req = Mysql.getInstance().prepareStatement(SQL_LOAD);
			req.setString(1,libelle);
			ResultSet res = req.executeQuery();
			ArrayList<Article> articles = new ArrayList<Article>();
			while(res.next())
			{
				Article a = new Article(res.getInt("reference"),res.getString("libelle"),res.getDouble("prixUnitaire"));
				articles.add(a);
			}
			return articles;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}


	@Override
	public void save(Article article) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		try {
			if(article.getReference()!=-1)
				throw new IllegalArgumentException();

			PreparedStatement req = Mysql.getInstance().prepareStatement(SQL_SAVE);
			req.setString(1, article.getLibelle());
			req.setDouble(2, article.getPrixUnitaire());

			req.execute();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}

	@Override
	public void saveOrUpdate(Article article) {
		// TODO Auto-generated method stub

		try {
			PreparedStatement req;
			if(article.getReference()==-1)
				this.save(article);
			else
			{
				req = Mysql.getInstance().prepareStatement(SQL_UPDATE);
				req.setString(1, article.getLibelle());
				req.setDouble(2, article.getPrixUnitaire());
				req.setInt(3, article.getReference());

				req.execute();
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void remove(Article article) {
		// TODO Auto-generated method stub
		if(article.getReference()==-1)
			throw new IllegalArgumentException();

		try {
			PreparedStatement req = Mysql.getInstance().prepareStatement(SQL_DELETE);
			req.setInt(1, article.getReference());
			int n = req.executeUpdate();
			if(n==1)
				article.setReference(-1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
